import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:shop/http/http_util.dart';
import 'package:shop/http/request_url.dart';
import 'package:shop/http/response_result.dart';

class FutureBuilderScreen extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
     return Center(
       child: FutureBuilder<Result>(
         future: getCarousel(),
         builder: (BuildContext context, AsyncSnapshot<Result> snapshot){
           //请求已结束
           if(snapshot.connectionState == ConnectionState.done){
             print("hasError:${snapshot.hasError}");
             print("error:${snapshot.error}");
             if(snapshot.hasError){
               return Text("error:${snapshot.error}");
             }else{
               return Text("Contents:${snapshot.data.data.toString()}");
             }
           }else{
             //请求未结束，显示loading
             return CircularProgressIndicator();
           }
         },
       ),
     );
  }

  Future<Result> getCarousel() async {
    final response = await HttpUtils().postFuture(RequestUrl().homeCarousel, data: null);

    Map<String, dynamic> map = json.decode(response.data);
    return Result.fromJson(map);
  }
}